×Welcome to Q&A for professional and enthusiast programmers — check out
the FAQ </faq>!
Stack Exchange
log in
</users/login?returnurl=%2fquestions%2f3151450%2fgoogle-geolocation-api-use-longitude-and-latitude-to-get-address>
| careers <http://careers.stackoverflow.com> | chat
<http://chat.stackoverflow.com> | meta <http://meta.stackoverflow.com> |
about </about> | faq </faq>


Stack Overflow </>

  * Questions </questions>
  * Tags </tags>
  * Users </users>
  * Badges </badges>
  * Unanswered </unanswered>

  * Ask Question </questions/ask>


  Google geolocation API - Use longitude and latitude to get address?
  </questions/3151450/google-geolocation-api-use-longitude-and-latitude-to-get-address>

<http://engine.adzerk.net/redirect/0/5922/2444/8277/1630b9024efe404b87d25b20f483feaf/43/1178/8808/634529154265634621?keywords=c%23%2cgoogle%2cgoogle-maps%2cgeolocation>
up vote 1 down vote favorite <#>
**
share [fb] share [tw]
	

I have noticed a lot of information about how to get your location using
Google geolocation looks, based on IP. But I am wondering if and how I
could use this service to input a location (longitude and latitude) and
get back the current address, or at least a city, state.

I would like to do this in C#, but I'll work with any language.

Any advice? Brett

c# </questions/tagged/c%23> google </questions/tagged/google>
google-maps </questions/tagged/google-maps> geolocation
</questions/tagged/geolocation>
link </q/3151450>|improve this question </posts/3151450/edit>
	
edited Jun 30 '10 at 17:06 </posts/3151450/revisions>
</users/83406/jason-hall>
Jason Hall </users/83406/jason-hall>
8,7421123
	
asked Jun 30 '10 at 16:45
</users/304684/brett>
Brett </users/304684/brett>
693115

79% accept rate

	
	

feedback


    4 Answers

active
</questions/3151450/google-geolocation-api-use-longitude-and-latitude-to-get-address?answertab=active#tab-top>
oldest
</questions/3151450/google-geolocation-api-use-longitude-and-latitude-to-get-address?answertab=oldest#tab-top>
votes
</questions/3151450/google-geolocation-api-use-longitude-and-latitude-to-get-address?answertab=votes#tab-top>

up vote 3 down vote accepted
	

What you describe is called Reverse Geocoding
<http://en.wikipedia.org/wiki/Reverse_geocoding>. Google provides a
Geocoding Web Service API
<http://code.google.com/apis/maps/documentation/geocoding/> which you
can call from your server-side application (using any language) to do
reverse geocoding
<http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding>.

For example, the following request:

    http://maps.google.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false

... will return a response that looks like the following (truncated):

|<GeocodeResponse> 
 <status>OK</status> 
 <result> 
  <type>street_address</type> 
  <formatted_address>277 Bedford Ave, Brooklyn, NY 11211, USA</formatted_address> 
  <address_component> 
   <long_name>277</long_name> 
   <short_name>277</short_name> 
   <type>street_number</type> 
  </address_component> 
  <address_component> 
   <long_name>Bedford Ave</long_name> 
   <short_name>Bedford Ave</short_name> 
   <type>route</type> 
  </address_component> 
  <address_component> 
   <long_name>Brooklyn</long_name> 
   <short_name>Brooklyn</short_name> 
   <type>sublocality</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>New York</long_name> 
   <short_name>New York</short_name> 
   <type>locality</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>Kings</long_name> 
   <short_name>Kings</short_name> 
   <type>administrative_area_level_2</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>New York</long_name> 
   <short_name>NY</short_name> 
   <type>administrative_area_level_1</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>United States</long_name> 
   <short_name>US</short_name> 
   <type>country</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>11211</long_name> 
   <short_name>11211</short_name> 
   <type>postal_code</type> 
  </address_component> 
  <geometry> 
   <location> 
    <lat>40.7142330</lat> 
    <lng>-73.9612910</lng> 
   </location> 
   <location_type>ROOFTOP</location_type> 
   <viewport> 
    <southwest> 
     <lat>40.7110854</lat> 
     <lng>-73.9644386</lng> 
    </southwest> 
    <northeast> 
     <lat>40.7173806</lat> 
     <lng>-73.9581434</lng> 
    </northeast> 
   </viewport> 
  </geometry> 
 </result> 
</GeocodeResponse> 
|

However be aware that the Google Maps API Terms of Use
<http://code.google.com/apis/maps/terms.html> seem to prohibit the
storage of the results
<http://code.google.com/apis/maps/terms.html#section_10_12>, unless the
store acts as a cache for data that will used in Google Maps. You may
want to get in touch with Google and enquire on the Google Maps API
Premier <http://www.google.com/enterprise/earthmaps/maps.html> to have
more flexible terms of use for your geocoding requirements.

link
</questions/3151450/google-geolocation-api-use-longitude-and-latitude-to-get-address/3151512#3151512>|improve
this answer </posts/3151512/edit>
	
answered Jun 30 '10 at 16:54
</users/222908/daniel-vassallo>
Daniel Vassallo </users/222908/daniel-vassallo>
64.2k777123

	
	
Works like a charm thanks. I won't be storing the data, so that
shouldn't be an issue. Thanks!!! – Brett </users/304684/brett> Jun 30
'10 at 18:55

feedback

<http://engine.adzerk.net/redirect/0/5917/2444/8277/133b223d006f4241b7b0a4d347eb4524/44/1178/8803/634529154265790867?keywords=c%23%2cgoogle%2cgoogle-maps%2cgeolocation>
up vote 1 down vote
	

Here is a c# implementation. Please note that I am no c# programmer - so
the code might be ugly. But it works. It gives you more than just the
address. This is a console application, you should be able to adapt it
for webforms or winforms easily.

|using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.XPath;

namespace ReverseGeoLookup
{
 // http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding
    public static string ReverseGeoLoc(string longitude, string latitude,
        out string Address_ShortName,
        out string Address_country,
        out string Address_administrative_area_level_1,
        out string Address_administrative_area_level_2,
        out string Address_administrative_area_level_3,
        out string Address_colloquial_area,
        out string Address_locality,
        out string Address_sublocality,
        out string Address_neighborhood)
    {

        Address_ShortName = "";
        Address_country = "";
        Address_administrative_area_level_1 = "";
        Address_administrative_area_level_2 = "";
        Address_administrative_area_level_3 = "";
        Address_colloquial_area = "";
        Address_locality = "";
        Address_sublocality = "";
        Address_neighborhood = "";

        XmlDocument doc = new XmlDocument();

        try
        {
            doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&sensor=false");
            XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
            if (element.InnerText == "ZERO_RESULTS")
            {
                return ("No data available for the specified location");
            }
            else
            {

                element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");

                string longname="";
                string shortname="";
                string typename ="";
                bool fHit=false;


                XmlNodeList xnList = doc.SelectNodes("//GeocodeResponse/result/address_component");
                foreach (XmlNode xn in xnList)
                {
                    try
                    {
                        longname = xn["long_name"].InnerText;
                        shortname = xn["short_name"].InnerText;
                        typename = xn["type"].InnerText;


                        fHit = true;
                        switch (typename)
                        {
                            //Add whatever you are looking for below
                            case "country":
                                {
                                    Address_country = longname;
                                    Address_ShortName = shortname;
                                    break;
                                }

                            case "locality":
                                {
                                    Address_locality = longname;
                                    //Address_locality = shortname; //Om Longname visar sig innehålla konstigheter kan man använda shortname istället
                                    break;
                                }

                            case "sublocality":
                                {
                                    Address_sublocality = longname;
                                    break;
                                }

                            case "neighborhood":
                                {
                                    Address_neighborhood = longname;
                                    break;
                                }

                            case "colloquial_area":
                                {
                                    Address_colloquial_area = longname;
                                    break;
                                }

                            case "administrative_area_level_1":
                                {
                                    Address_administrative_area_level_1 = longname;
                                    break;
                                }

                            case "administrative_area_level_2":
                                {
                                    Address_administrative_area_level_2 = longname;
                                    break;
                                }

                            case "administrative_area_level_3":
                                {
                                    Address_administrative_area_level_3 = longname;
                                    break;
                                }

                            default:
                                fHit = false;
                                break;
                        }


                        if (fHit)
                        {
                            Console.Write(typename);
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.Write("\tL: " + longname + "\tS:" + shortname + "\r\n");
                            Console.ForegroundColor = ConsoleColor.Gray;
                        }
                    }

                    catch (Exception e)
                    {
                        //Node missing either, longname, shortname or typename
                        fHit = false;
                        Console.Write(" Invalid data: ");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("\tX: " + xn.InnerXml + "\r\n");
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }


                }

                //Console.ReadKey();
                return (element.InnerText);
            }

        }
        catch (Exception ex)
        {
            return ("(Address lookup failed: ) " + ex.Message);
        }
        }
}
|

link
</questions/3151450/google-geolocation-api-use-longitude-and-latitude-to-get-address/4484904#4484904>|improve
this answer </posts/4484904/edit>
	
answered Dec 19 '10 at 20:21
</users/547942/bob>
bob </users/547942/bob>
111

	
	

feedback

up vote 0 down vote
	

Google's Maps APIs goes over http, so sending request with get and then
parsing the request... It should be possible to do in any language.

For example, in PHP:

|$ret = file_get_contents("http://maps.google.com/maps/api/geocode/xml?address=" .
            urlencode($address) .
            "&sensor=false" .
            "&key=" . $this->key
    );

$xml = new SimpleXMLElement($ret);
$error = $xml->status;
|

Same works for all APIs.

link
</questions/3151450/google-geolocation-api-use-longitude-and-latitude-to-get-address/3151494#3151494>|improve
this answer </posts/3151494/edit>
	
answered Jun 30 '10 at 16:51
</users/345959/smar>
Smar </users/345959/smar>
692411

	
	
This is not an implementation of reverse geocoding. – igorw
</users/289985/igorw> Jun 30 '10 at 16:59
	
I meant as example, how whole thing worked... Maybe I should've been
more exact. I thought that just showing off how it actually works would
be enough. – Smar </users/345959/smar> Jun 30 '10 at 17:05

feedback

up vote 0 down vote
	

What you are trying to do is reverse geocoding (See Daniel's answer).

An example implementation in PHP:

|/**
* reverse geocoding via google maps api
* convert lat/lon into a name
*/
function reverse_geocode($lat, $lon) {
    $url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";
    $data = json_decode(file_get_contents($url));
    if (!isset($data->results[0]->formatted_address)){
        return "unknown Place";
    }
    return $data->results[0]->formatted_address;
}
|

link
</questions/3151450/google-geolocation-api-use-longitude-and-latitude-to-get-address/3151543#3151543>|improve
this answer </posts/3151543/edit>
	
answered Jun 30 '10 at 16:57
</users/289985/igorw>
igorw </users/289985/igorw>
2,579311

	
	

feedback


    Your Answer

  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
*

 
draft saved
log in
</users/login?returnurl=%2fquestions%2f3151450%2fgoogle-geolocation-api-use-longitude-and-latitude-to-get-address>
	
or
	
Name
Email required, but never shown
Home Page


    Not the answer you're looking for? Browse other questions tagged c#
    </questions/tagged/c%23> google </questions/tagged/google>
    google-maps </questions/tagged/google-maps> geolocation
    </questions/tagged/geolocation> or ask your own question
    </questions/ask>.


        Hello World!

This is a collaboratively edited question and answer site for
*professional and enthusiast programmers*. It's 100% free, no
registration required.

about » </about>   faq » </faq>

tagged

c# </questions/tagged/c%23> × 220801
google-maps </questions/tagged/google-maps> × 5918
google </questions/tagged/google> × 5596
geolocation </questions/tagged/geolocation> × 1606

asked

	

*1 year ago*

viewed

	

*2,238 times*

active

	

*7 months ago <?lastactivity>*

Something like \enumerate, but with custom numbers at each \item
<http://engine.adzerk.net/redirect/0/3194/3013/8277/a590d802e57947feacc2b023b426122b/45/1178/3561/634529154265790867?keywords=c%23%2cgoogle%2cgoogle-maps%2cgeolocation&url=http%3A%2F%2Ftex.stackexchange.com%2Fquestions%2F29850%2Fsomething-like-enumerate-but-with-custom-numbers-at-each-item>

Careers 2.0 <http://careers.stackoverflow.com/jobs?a=12>

Game Lead/Engineering Manager
Lolapps San Francisco, CA
<http://careers.stackoverflow.com/jobs/13474/game-lead-engineering-manager-lolapps?a=fV07fDG>


Senior Software Engineer (Backend)
Comprehend Systems Palo Alto, CA
<http://careers.stackoverflow.com/jobs/13992/senior-software-engineer-backend-comprehend-systems?a=gwWGHGo>


Experienced Software Developer– OO, Agile, C#, .NET
thetrainline.com London, England
<http://careers.stackoverflow.com/jobs/13687/experienced-software-developer-oo-agile-c-sharp-thetrainline-com?a=gaBucVy>



        Related

What's the simplest way to get a user's latitude and longitude from an
ip address
</questions/1576423/whats-the-simplest-way-to-get-a-users-latitude-and-longitude-from-an-ip-address>

Google maps API can't get the longitude
</questions/1823680/google-maps-api-cant-get-the-longitude>
Get IP Address Information such as Country, LatLong, City using Google
Maps Data API
</questions/2360584/get-ip-address-information-such-as-country-latlong-city-using-google-maps-data>

Using Latitude & Longitude to get a place with Yahoo
</questions/2502912/using-latitude-longitude-to-get-a-place-with-yahoo>
How retrieve latitude and longitude via Google Maps API?
</questions/2770421/how-retrieve-latitude-and-longitude-via-google-maps-api>

Google maps sometimes does not return a geocoded value for string
</questions/2930623/google-maps-sometimes-does-not-return-a-geocoded-value-for-string>

get latitude longitude based on address api
</questions/3519675/get-latitude-longitude-based-on-address-api>
How to get client's Geolocation (Latitude & Longitude) extremely
QUICKLY?
</questions/3753416/how-to-get-clients-geolocation-latitude-longitude-extremely-quickly>

Google map Geolocation formatting address
</questions/3861736/google-map-geolocation-formatting-address>
Get country from latitude longitude
</questions/4497728/get-country-from-latitude-longitude>
Google Maps API using jQuery Plugin: How to get a marker's latitude and
longitude on click?
</questions/4512369/google-maps-api-using-jquery-plugin-how-to-get-a-markers-latitude-and-longitude>

Google Maps API - use “search nearby” feature?
</questions/4707417/google-maps-api-use-search-nearby-feature>
Use Google Gears geolocation from a python app
</questions/4792008/use-google-gears-geolocation-from-a-python-app>
Google's Geolocation API (“My Location”) Commercial License
</questions/4909519/googles-geolocation-api-my-location-commercial-license>
Google's Geolocation API and “My Location” feature
</questions/4950563/googles-geolocation-api-and-my-location-feature>
How can I use Google API to obtain the Geolocation of an IP address?
</questions/5181897/how-can-i-use-google-api-to-obtain-the-geolocation-of-an-ip-address>

Google Api Get nearest locations
</questions/5232242/google-api-get-nearest-locations>
GeoLocation + Google Maps API?
</questions/5233558/geolocation-google-maps-api>
Google map localization </questions/5503524/google-map-localization>
Interacting with Google's Geolocation Detection
</questions/5742269/interacting-with-googles-geolocation-detection>
google maps Get Latitude and Longitude from zip code
</questions/6100264/google-maps-get-latitude-and-longitude-from-zip-code>
Adding Placemarks on Google Earth API
</questions/6699361/adding-placemarks-on-google-earth-api>
latitude and longitude can find zip code?
</questions/6764917/latitude-and-longitude-can-find-zip-code>
How to easily get a zipcode from a generic address with google maps api?
</questions/6888185/how-to-easily-get-a-zipcode-from-a-generic-address-with-google-maps-api>

How to convert postcode to Geolocation(latitude & longitutde) using
google maps api?
</questions/7075912/how-to-convert-postcode-to-geolocationlatitude-longitutde-using-google-maps-a>

question feed </feeds/question/3151450>
lang-cs
about </about> | faq </faq> | new blog <http://blog.stackexchange.com> |
chat <http://chat.stackoverflow.com> | data
<http://data.stackexchange.com> | podcast
<http://blog.stackoverflow.com/category/podcasts/> | shop
<http://shop.stackexchange.com/> | legal
<http://stackexchange.com/legal> | advertising info
<http://engine.adzerk.net/redirect/0/2776/2751/0/4de3c60f719c4dfcb1a57531c7050090/0>
| mobile | *contact us <mailto:team@stackoverflow.com>* | *feedback
<http://meta.stackoverflow.com>*
■ stackoverflow.com <http://stackoverflow.com>  ■ api/apps
<http://stackapps.com>  ■ careers <http://careers.stackoverflow.com> 
■ serverfault.com <http://serverfault.com>  ■ superuser.com
<http://superuser.com>  ■ meta <http://meta.stackoverflow.com> 
■ area 51 <http://area51.stackexchange.com>  ■ webapps
<http://webapps.stackexchange.com>  ■ gaming
<http://gaming.stackexchange.com>  ■ ubuntu <http://askubuntu.com> 
■ webmasters <http://webmasters.stackexchange.com>  ■ cooking
<http://cooking.stackexchange.com>  ■ game development
<http://gamedev.stackexchange.com>  ■ math
<http://math.stackexchange.com>  ■ photography
<http://photo.stackexchange.com>  ■ stats
<http://stats.stackexchange.com>  ■ tex <http://tex.stackexchange.com> 
■ english <http://english.stackexchange.com>  ■ theoretical cs
<http://cstheory.stackexchange.com>  ■ programmers
<http://programmers.stackexchange.com>  ■ unix
<http://unix.stackexchange.com>  ■ apple
<http://apple.stackexchange.com>  ■ wordpress
<http://wordpress.stackexchange.com>  ■ physics
<http://physics.stackexchange.com>  ■ home improvement
<http://diy.stackexchange.com>  ■ gis <http://gis.stackexchange.com> 
■ electronics <http://electronics.stackexchange.com>  ■ android
<http://android.stackexchange.com>  ■ security
<http://security.stackexchange.com>  ■ bicycles
<http://bicycles.stackexchange.com>  ■ dba
<http://dba.stackexchange.com>  ■ drupal
<http://drupal.stackexchange.com>  ■ sharepoint
<http://sharepoint.stackexchange.com> 
<http://www.peer1.com/stackoverflow>
<http://creativecommons.org/licenses/by-sa/3.0/>
rev 2011.9.29.2
site design / logo © 2011 stack exchange inc; user contributions
licensed under cc-wiki <http://creativecommons.org/licenses/by-sa/3.0/>
with attribution required
<http://blog.stackoverflow.com/2009/06/attribution-required/>
